#include "user_global.h"
#include "crc.h"
#include <string.h>
#include "stdlib.h"
#include "device_usart.h"
#include "user_device.h"
#include "usart.h"
#include "user_config.h"
#include "tim.h"
#include "speed_cntr.h"
#include "modbus.h"


void HAL_UART_RxCpltCallback(UART_HandleTypeDef huart)
{
    if(huart == USART2)
    {
        if(!REN0)
            return;
        uart0_rx_pending = 1;
        if (timer_flag_get(TIMER5, TIMER_FLAG_UP))
        {
            uart0_len = 0;
        }
        timer_counter_value_config(TIMER5, 65536-2000);
        timer_flag_clear(TIMER5, TIMER_FLAG_UP);
        uart0_buf[UART0_BUF_MASK & uart0_len++] = (uchar)usart_data_receive(USART2);

        uart0_timestamp = timer_counter_read(TIMER5);
    }

    if(huart == USART1)
    {
        // 超时,一帧数据接收完成
        if(SystemTick() - uart1_timestamp_long >= MAX_BYTE_TO_BYTE_DELAY)
            uart1_rx_len = 0;


        uart1_rx_buf[UART1_BUF_MASK & uart1_rx_len++] = (uchar)usart_data_receive(USART1);


    #ifdef BCL95
        // 判断起始字符
        if(uart1_rx_buf[UART1_BUF_MASK & (uart1_rx_len - 1)] == STX)
        {
            uart1_rx_buf[0] = STX;
            uart1_rx_len = 1;
        }
    #endif
        // 判断结束字符
        if(uart1_rx_len > 2 && !check_reply)
        {
            if((uart1_rx_buf[uart1_rx_len - 2] == CR &&
                uart1_rx_buf[uart1_rx_len - 1] == LF))
            {
                if(uart1_rx_len < (MAX_BAR_LEN + 3))
                {
                    uart1_rx_buf[uart1_rx_len] = 0x00;
                    Append_Bar((char*)uart1_rx_buf,uart1_rx_len + 1);
                    uart1_rx_len = 0;
                    ++ro_word.barCount;
                    Start_Beep(BEEP_INFO);
                }
                else
                {
                    uart1_rx_len = 0;
                    Start_Beep(BEEP_WARNING);
                }
            }
        }
        uart1_timestamp_long = SystemTick();
    }
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef huart)
{
    if(huart == USART2)
    {
        if(uart0_len)
        {
            usart_data_transmit(USART2, *uart0_tx_ptr++);
            --uart0_len;
        }
        else{
            uart0_tx_pending = 0;
            usart_interrupt_disable(USART2, USART_INT_TBE);
        }
    }

    if(huart == USART1)
    {
        if(uart1_tx_len)
        {
            usart_data_transmit(USART1, *uart1_tx_ptr++);
            --uart1_tx_len;
        }
        else{
            uart1_tx_pending = 0;
            uart1_rx_pending = 1;
            uart1_timestamp = SystemTickLow();
            uart1_timestamp_long = SystemTick();
            usart_interrupt_disable(USART1, USART_INT_TBE);
        }
    }
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef htim)
{
  if(htim == TIMER0) {
      speed_update(T1);
  }
  if(htim == TIMER1) {
      speed_update(S1);
  }
  if(htim == TIMER11) {
      speed_update(S2);
  }
  if(htim == TIMER7) {
      speed_update(V2);
  }
  if(htim == TIMER8) {
      speed_update(V3);
  }
  if(htim == TIMER9) {
      speed_update(V1);
  }
  if(htim == TIMER6) {
      ++system_tick.UU16;
  }

}



